package com.timber.util;

import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;

/**
 * Base class to hold rows of data from DB.
 */

public class SLDataSetAdaptor implements SLDataSet {
	protected Hashtable byHashKey = new Hashtable();

	protected Vector keyVector = new Vector();

	protected Vector displayVector = new Vector(); // ALL in first pos

	protected Vector dataVector = new Vector();

	protected String sql = null;

	protected String sproc = null;

	protected String clName = null; // class name for the objects

	protected Vector procParams = new Vector();

	protected String _name = null; // name of the set

	protected Class _class = null; // row's class

	public SLDataSetAdaptor(String className, String sSQL) {
		this(className, "DEFAULT", sSQL);
	}

	public SLDataSetAdaptor(String className, String aName, String sSQL) {
		this(className, aName, sSQL, sSQL);
	}

	public SLDataSetAdaptor(String className, String aName, String sSQL,
			String sProcs) {
		clName = className;
		_name = aName;
		sql = sSQL;
		sproc = sProcs;
	}

	public SLDataSetAdaptor(String className, String aName, String sSQL,
			Vector vProcs) {
		this(className, aName, sSQL);
		procParams = vProcs;
	}

	public SLDataSetAdaptor(SLDataSet aDSA) {
		this(aDSA.getClassName(), aDSA.getName(), aDSA.getSQL());
		Vector tV = aDSA.getDataVector();

		for (Enumeration en = tV.elements(); en.hasMoreElements();) {
			this.put((SLDataObject) en.nextElement());
		}
	}

	public SLDataSetAdaptor() {
		clName = _name = sql = sproc = null;
	}

	public void setClassName(String s) {
		clName = s;
		_class = null;
	}

	public String getClassName() {
		return clName;
	}

	public SLDataObject getRowInstance() throws Exception {
		if (_class == null) {
			try {
				_class = Class.forName(clName);
			} catch (ClassNotFoundException ce) {
				Util.error(ce.toString());
				throw new Exception("Failed to create class  " + clName);
			}
		}

		return (SLDataObject) _class.newInstance();
	}

	public void setName(String s) {
		_name = s;
	}

	public String getName() {
		return _name;
	}

	public void setSQL(String s) {
		sql = s;
	}

	public String getSQL() {
		return sql;
	}

	public void setProcs(String aS) {
		sproc = aS;
	}

	public String getProcs() {
		return sproc;
	}

	public void clearProcP() {
		procParams = new Vector();
	}

	public void addProcP(Object o) {
		procParams.add(o);
	}

	public void setProcParams(Vector aV) {
		procParams = new Vector(aV);
	}

	// public String getProcParams() - below

	public Object getByKey(Object key) {
		if (byHashKey.isEmpty())
			return null;
		return (byHashKey.get(key));
	}

	public void put(SLDataObject aO) {
		Object key = aO.hashKey();
		if (key != null) {
			byHashKey.put(key, aO);
			keyVector.add(key);
			displayVector.add(aO.listString());
		}
		dataVector.add(aO);
	}

	public void putUnique(SLDataObject aDO) {
		Object temp = null;
		Object key = aDO.hashKey();
		if (key != null) {
			temp = byHashKey.get(key);
			byHashKey.put(key, aDO);
			if (!keyVector.contains(key)) {
				keyVector.add(key);
			}
			if (!displayVector.contains(aDO.listString()))
				displayVector.add(aDO.listString());
		}
		if (temp != null) {
			int i = dataVector.indexOf(temp);
			dataVector.remove(i);
			dataVector.insertElementAt(aDO, i);
		} else {
			dataVector.add(aDO);
		}
	}

	public void displayAdd(Object aO) {
		displayVector.add(aO);
	}

	public void clear() {
		byHashKey = new Hashtable();
		keyVector = new Vector();
		displayVector = new Vector();
		dataVector = new Vector();
	}

	public Vector getKeyList() {
		if (byHashKey.isEmpty())
			return null;

		return keyVector;
	}

	public Vector getDisplayItems() {
		if (byHashKey.isEmpty())
			return null;

		return displayVector;
	}

	public Vector getDataVector() {
		return dataVector;
	}

	public int size() {
		return dataVector.size();
	}

	public boolean isEmpty() {
		return byHashKey.isEmpty();
	}

	/**
	 * remove item from manager
	 */

	public void removeItem(SLDataObject o) {
		if (o == null || !dataVector.contains(o))
			return;

		byHashKey.remove(o.hashKey());
		keyVector.removeElement(o.hashKey());
		displayVector.removeElement(o.listString());
		dataVector.removeElement(o);
	}

	public void removeItemAt(int indx) {
		SLDataObject o = (SLDataObject) dataVector.elementAt(indx);

		removeItem(o);
	}

	public String getProcParams() {
		int i;
		StringBuffer buff = new StringBuffer(1000);

		for (i = 0; i < procParams.size(); i++) {
			Object o = procParams.elementAt(i);

			if (i > 0)
				buff.append(",");
			else
				buff.append(" ");

			if (o == null)
				buff.append("null");
			else if (o instanceof String)
				buff.append("'").append(o).append("'");
			else if (o instanceof Number)
				buff.append(o.toString());
			else if (o instanceof java.util.Date)
				buff.append("'").append(Global.dateOnly((java.util.Date) o))
						.append("'");
			// buff.append("'").append(DateFormat.getDateInstance(DateFormat.SHORT).format(o)).append("'");
			else if (o instanceof Boolean) {
				if (o.equals(Boolean.FALSE))
					buff.append("0");
				else
					buff.append("1");
			} else
				Util.error("Unknown Proc Parameter Type   " + o);
		}

		return buff.toString();
	}

}
